package com.rabbit.library.utils;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 */
public class TimePickerUtils {


    public static void initTimePicker(Context context, OnChangeListener onChangeListener) {//Dialog 模式下，在底部弹出
        TimePickerView pvTime = new TimePickerBuilder(context, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                onChangeListener.onDatePicked(getTime(date));
                Log.i("pvTime", "onTimeSelect");
            }
        })
                .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged(Date date) {
                        Log.i("pvTime", "onTimeSelectChanged");
                    }
                })
                .setType(new boolean[]{true, true, true, false, false, false})
                .isDialog(true) //默认设置false ，内部实现将DecorView 作为它的父控件。
                .addOnCancelClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.i("pvTime", "onCancelClickListener");
                    }
                })
                .setItemVisibleCount(5) //若设置偶数，实际值会加1（比如设置6，则最大可见条目为7）
                .setLineSpacingMultiplier(2.0f)
                .isAlphaGradient(true)
                .build();

        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
                dialogWindow.setDimAmount(0.3f);
            }
        }
        pvTime.show();
    }

    public static void initTimePicker(Context context,String time, OnChangeListener onChangeListener) {//Dialog 模式下，在底部弹出

        Calendar calendar = Calendar.getInstance();	//创建一个Calendar类型的对象
        try {
            Date date = sdf.parse(time);	//解析日期字符串，生成Date对象
            calendar.setTime(date);			//使用Date对象设置此Calendar对象的时间
        } catch (ParseException e) {
            e.printStackTrace();
        }

        TimePickerView pvTime = new TimePickerBuilder(context, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                onChangeListener.onDatePicked(getTime(date));
                Log.i("pvTime", "onTimeSelect");
            }
        })
                .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged(Date date) {
                        Log.i("pvTime", "onTimeSelectChanged");
                    }
                })
                .setDate(calendar)
                .setType(new boolean[]{true, true, true, false, false, false})
                .isDialog(true) //默认设置false ，内部实现将DecorView 作为它的父控件。
                .addOnCancelClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.i("pvTime", "onCancelClickListener");
                    }
                })
                .setItemVisibleCount(5) //若设置偶数，实际值会加1（比如设置6，则最大可见条目为7）
                .setLineSpacingMultiplier(2.0f)
                .isAlphaGradient(true)
                .build();

        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
                dialogWindow.setDimAmount(0.3f);
            }
        }
        pvTime.show();
    }

    private static String getTime(Date date) {//可根据需要自行截取数据显示
        Log.d("getTime()", "choice date millis: " + date.getTime());
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }


    static String defaultDay = "2020-07-01";//默认开始时间为7月1日

    public static String getStartDay() {
//        String startDay = (String) SharePreferenceUitls.get(BaseApplication.getContext(), Constant.START_TIME, "");
        String startDay = "";
        if (TextUtils.isEmpty(startDay)) {
            startDay = getStartTime();
        }
        return startDay;
    }

    public static String getEndDay() {
//        String endDay = (String) SharePreferenceUitls.get(BaseApplication.getContext(), Constant.END_TIME, "");
        String endDay = "";
        if (TextUtils.isEmpty(endDay)) {
            endDay = getEndTime();
        }
        return endDay;
    }

    public static String getStartTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 13);
        Date selectDate = calendar.getTime();//选择开始日期
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = defaultDay;
        try {
            Date defaultDate = format.parse(defaultDay);
            if (selectDate.after(defaultDate)) {
                result = format.format(selectDate);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String getEndTime() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR));
        Date selectDate = calendar.getTime();//选择开始日期
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result = defaultDay;
        try {
            Date defaultDate = format.parse(defaultDay);
            if (selectDate.after(defaultDate)) {
                result = format.format(selectDate);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String getTodayTime() {
        return sdf.format(new Date().getTime());
    }

    static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    // 获得本周一0点时间
    @SuppressLint("WrongConstant")
    public static String getTimesWeekmorning() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        return sdf.format(cal.getTime());
    }

    // 获得本周日24点时间
    @SuppressLint("WrongConstant")
    public static String getTimesWeeknight() {
        Calendar cal = Calendar.getInstance();
        cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.setTime(cal.getTime());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        return sdf.format(cal.getTime());
    }

    // 获得本月第一天0点时间
    public static String getTimesMonthmorning() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH,1);
        calendar.add(Calendar.MONTH,0);
        return sdf.format(calendar.getTime());
    }

    // 获得本月最后一天24点时间
    public static String getTimesMonthnight() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE, calendar.getActualMaximum(Calendar.DATE));
        return sdf.format(calendar.getTime());
    }

    public interface OnChangeListener {
        void onDatePicked(String year_month_day);
    }
}
